// packageG/mycomponent/horizon/horizon.js
const app = getApp();
var buleHandle = require("../../../utils/horizonBule");
Component({
  behaviors: [buleHandle],
  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    btnFlag: {
      type: null
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    codeUrl: '',
    imgPath: app.globalData.imgPath,
  },
  lifetimes: {
    attached() {}
  },
  observers: {
    'datas': function (showA) {
      if (showA.ewm_box) {
        this.getCode()
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getCode() {
      //接口不对
      let urlStr = app.getNetAddresss("plugin.horizon.frontend.index.getStaffCode");
      app._postNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              codeUrl: res.data.code,
              show: true
            })
          } else {
            app.tips(res.msg)
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
    showTap() {
      this.setData({
        show: false
      })
    },
    scanCode() {
      let that = this;
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          let {
            result
          } = res;
          let str = result.substr(result.lastIndexOf('/') + 1);
          that.setData({
            strMac: str
          })
          wx.showModal({
            title: '是否连接当前设备?',
            content: str,
            complete: (res) => {
              if (res.confirm) {
                console.log(str)
                wx.showLoading({
                  title: '蓝牙开启中',
                })
                that.data.tojump = true;
                that.openBluetoothAdapter(str)
              }
            }
          })
        }
      })
    },
    convertToAndroidFormat(deviceId) {
      // Remove hyphens and convert to lowercase
      var formattedId = deviceId.replace(/-/g, "").toLowerCase();
      return formattedId;
    },
    // 查看已经连接的设备
    tapLook() {
      // let that = this;
      // wx.openBluetoothAdapter({
      //   success: (res) => {
      //     console.log(res)
      //     that.get_getBluetoothDevices();
      //   },
      //   fail: (err) => {
      //     if (res.errCode === 10001) {
      //       wx.showModal({
      //         title: '请开启蓝牙',
      //       })
      //       wx.onBluetoothAdapterStateChange(function (res) {
      //         console.log('onBluetoothAdapterStateChange', res)
      //         that.get_getBluetoothDevices();
      //       })
      //     } else {
      //       wx.hideLoading();
      //       wx.showModal({
      //         title: '提示',
      //         content: res.errMsg,
      //       })
      //     }
      //   }
      // })
      wx.navigateTo({
        url: '/mircoApp/horizon/deviceList/deviceList',
      })
    },
    get_getBluetoothDevices() {
      let that = this;
      wx.getBluetoothDevices({
        success: (res) => {
          console.log(res)
          let device = res.devices;
          let list = device.filter((item) => {
            return item.name == 'HUIYANXINGDONG' && item.advertisData
          })
          if (list.length <= 0) {
            app.tips('该设备没有运行')
            return
          }
          for (let i = 0; i < list.length; i++) {
            if (that.mac(list[i].advertisData) == 'FC58FA124A6C') {
              console.log(list[i], '对应的设备')
              that.reenter('FC58FA124A6C', list[i].deviceId, list[i].advertisServiceUUIDs[0])
            }
          }
        }
      })
    }
  }
})